package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author with葡萄成熟时
 * @version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * @description:  新增套餐
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 上午12:37 2024/11/8
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);


        setmealMapper.addSetmeal(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for(SetmealDish s :setmealDishes){
            s.setSetmealId(id);
        }

        setmealDishMapper.addSetmealDish(setmealDishes);


    }

    /**
     * @description: 分页查询
     * @param: [setmealPageQueryDTO]
     * @return: com.sky.result.Result<com.sky.result.PageResult>
     * @author Wait葡萄成熟时
     * @date: 下午12:31 2024/11/8
     */
    @Override
    public PageResult selectSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
      Page<SetmealVO> page= setmealMapper.selectSetmealPage(setmealPageQueryDTO);


        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * @description: 根据id查询套餐
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午12:50 2024/11/8
     */
    @Override
    public SetmealVO selectSetmealById(Long id) {

     Setmeal setmeal=   setmealMapper.selectSetmealById(id);
     List<SetmealDish> setmealDishes=  setmealDishMapper.selectSetmealDishBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * @description: 批量删除套餐
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午1:15 2024/11/8
     */
    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        setmealMapper.deleteSetmeal(ids);
        setmealDishMapper.deleteSetmealDishBySetmealId(ids);

    }

    /**
     * @description: 修改套餐
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午1:35 2024/11/8
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        //先删了再加回来，mapper多次用
        setmealDishMapper.deleteSetmealDishBySetmealId(Collections.singletonList(setmealDTO.getId()));
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.addSetmealDish(setmealDishes);


    }

    /**
     * @description: 套餐起售、停售
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午1:49 2024/11/8
     */
    @Override
    public void startOrNot(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.updateSetmeal(setmeal);
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {

        List<Setmeal> list= setmealMapper.selectSetmealByCategoryId(setmeal);

        return list;
    }
    /**
     * 根据套餐id查询包含的菜品列表
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> selectDishItemById(Long id) {
    //先从套餐表查出菜品id，再根据菜品id查菜品表的图片地址和描述，封装成DishItemVO

        List<SetmealDish> setmealDishes = setmealDishMapper.selectSetmealDishBySetmealId(id);

        List<DishItemVO> dishItemVOS=new ArrayList<>();

        for (SetmealDish setmealDish : setmealDishes) {
            //获取Dish表中的图片和描述信息
            Dish dish = dishMapper.selectById(setmealDish.getDishId());


            DishItemVO dishItemVO = new DishItemVO();
            dishItemVO.setImage(dish.getImage());
            dishItemVO.setDescription(dish.getDescription());
            BeanUtils.copyProperties(setmealDish,dishItemVO);
            dishItemVOS.add(dishItemVO);
        }

        return dishItemVOS;
    }
}
